{extend name="public/world"}
{block name="style"}
<style type="text/css">
.layui-btn-xs {
    height: 18px;
    line-height: 18px;
    padding: 0 5px;
    font-size: 12px;
}
.layui-table-tool {
    min-height: 30px;
    line-height: 30px;
    padding: 0 10px;
}
.row-in {background-color: #2b2b2b !important;color: #fff9ec}
.layui-input, .layui-select {
    height: 30px;
}
</style>
{/block}
{block name="container"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form layui-border-box layui-table-view">
                <div class="layui-table-tool">
                    <div class="layui-table-tool-temp">
                        <div class="layui-btn-group fl">
                            <button type="button" class="layui-btn layui-btn-xs open-all">全部展开</button>
                            <button type="button" class="layui-btn layui-btn-normal layui-btn-xs close-all">全部关闭</button>
                        </div>
                    </div>
                    <div class="layui-table-tool-self" style="top: 0;">
                        {if isset($sonAuth['luck'])}
                        {if $luck}
                        <a href="{:url('/erp.InitialData/luck')}" class="layui-btn layui-btn-warm layui-btn-xs j-ajax" confirm="确定要解除锁帐吗？">解除锁帐</a>
                        {else /}
                        <a href="{:url('/erp.InitialData/luck')}" class="layui-btn layui-btn-danger layui-btn-xs j-ajax" confirm="确定要进行锁帐吗？">锁帐</a>
                        {/if}
                        {/if}
                    </div>
                </div>
                <table class="layui-table" id="dataTable" lay-size="sm"></table>
            </div>
        </div>
    </div>
</div>
{/block}
{block name="script"}
<script>
    layui.use(['form','treeTable'], function() {
        var $ = layui.jquery
            , form = layui.form
            , treeTable = layui.treeTable;
        var	re = treeTable.render({
            elem: '#dataTable',
            data: {:json_encode($data)},
            icon_key: 'name',
            end: function(e){
                form.render();
            },
            cols: [
                {
                    key: 'name',
                    title: '名称',
                    width: '350px',
                    template: function(item){
                        if(item.level == 0){
                            return '<span style="color:red;">'+item.name+'</span>';
                        }else if(item.level == 1){
                            return '<span style="color:green;">'+item.name+'</span>';
                        } else {
                            return '<span>'+item.name+'</span>';
                        }
                    }
                },
                {
                    key: 'code',
                    title: '科目编码',
                    width: '100px',
                },
                {
                    key: 'debit',
                    title: '期初借方',
                    width: '100px',
                },
                {
                    key: 'credit',
                    title: '期初贷方',
                    width: '100px',
                },
                {
                    key: 'luck',
                    title: '操作',
                    width: '120px',
                    template: function(d){
                        var html = '';
                        if(!d.son) {
                            {if isset($sonAuth['edit'])}
                                if(!d.luck) html += '<button class="layui-btn layui-btn-xs b-iframe-pop">修改</button>';
                            {/if}
                        }
                        return html;
                    }
                }
            ]
        });
        // 全部展开
        $('.open-all').click(function(){
            treeTable.openAll(re);
        })
        // 全部关闭
        $('.close-all').click(function(){
            treeTable.closeAll(re);
        })
        $('.b-iframe-pop').click(function() {
            var that = $(this);
            if(that.hasClass('layui-btn-danger')) {
                var code = that.parents('tr').find('.code').val(),
                    debit = that.parents('tr').find('.debit').val(),
                    credit = that.parents('tr').find('.credit').val();
                if(typeof(code) == 'undefined' || typeof(debit) == 'undefined' || typeof(credit) == 'undefined' ) {
                    layer.msg('页面错误，请刷新后重试');
                    setTimeout(function() {
                        window.location.reload();
                    }, 1500);
                    return false;
                }
                that.removeClass('layui-btn-danger').text('修改');
                that.parents('tr').removeClass('row-in').find('.layui-table-edit').remove();
                layer.load();
                $.ajax({
                    type: 'POST',
                    url: "{:url('/erp.InitialData/edit')}",
                    data: {code:code,debit:debit,credit:credit},
                    dataType: "json",
                    success:function(res) {
                        if(res.msg) {
                            layer.msg(res.msg);
                        }
                        if (res.code == 0) {
                            layer.closeAll('loading');
                        }
                        if (res.code == 1) {
                            setTimeout(function() {
                                layer.closeAll(); //关闭弹层
                                window.location.reload();
                            }, 1500);
                        }
                    }
                });
            } else {
                that.parents('tr').addClass('row-in').find('[data-field]').each(function () {
                    var field = $(this).attr('data-field');
                    if (field == 'code') {
                        $(this).append('<input type="hidden" name="code" class="code layui-table-edit" value="' + $(this).find('.layui-table-cell').text() + '">')
                    }
                    if (field == 'debit') {
                        $(this).append('<input type="text" name="debit" autocomplete="off" class="debit layui-input layui-table-edit" value="' + $(this).find('.layui-table-cell').text() + '">')
                    }
                    if (field == 'credit') {
                        $(this).append('<input type="text" name="credit" autocomplete="off" class="credit layui-input layui-table-edit" value="' + $(this).find('.layui-table-cell').text() + '">')
                    }
                    that.addClass('layui-btn-danger').text('保存');
                })
            }
        });
    });
</script>
{/block}